package com.ali.Reggie.service.impl;

import com.ali.Reggie.common.CustomException;
import com.ali.Reggie.entity.Category;
import com.ali.Reggie.entity.Dish;
import com.ali.Reggie.entity.Employee;
import com.ali.Reggie.entity.Setmeal;
import com.ali.Reggie.mapper.CategoryMapper;
import com.ali.Reggie.mapper.EmployeeMapper;
import com.ali.Reggie.service.CategoryService;
import com.ali.Reggie.service.DishService;
import com.ali.Reggie.service.EmployeeService;
import com.ali.Reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/9 14:49
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    //根据id来删除分类，删除之前进行判断

    @Override
    public void remove(Long id) {
        //1.查询当前分类是否关联菜品
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(lambdaQueryWrapper);
        if(count1 > 0){
            //说明关联了菜品
            throw new CustomException("当前分类已关联菜品，不能删除");
        }


        //2.查询当前分类是否关联套餐
        LambdaQueryWrapper<Setmeal> SetmealQueryWrapper = new LambdaQueryWrapper<>();
        SetmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(SetmealQueryWrapper);
        if(count2 > 0){
            //说明关联了套餐
            throw new CustomException("当前分类已关联套餐，不能删除");
        }

        //3.正常删除
        super.removeById(id);
    }
}